package com.burningtower.controller;

import com.burningtower.common.Result;
import com.burningtower.service.IOrganizationService;
import com.burningtower.service.IResourceService;
import com.burningtower.service.IRoleService;
import com.burningtower.service.IUserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@Controller
public class ViewController {
    @Autowired
    private IResourceService resourceService;
    @Autowired
    private IRoleService roleService;
    @Autowired
    private IUserService userService;
    @Autowired
    private IOrganizationService organizationService;

    @RequestMapping("/")
    public String welcome(){
        return "index";
    }
    @RequestMapping("/signIn")
    public String signIn(){
        return "login";
    }
    @PostMapping("/login")
    @ResponseBody
    public Result login(String username, String password, Boolean rememberMe){
        Subject subject = SecurityUtils.getSubject();
        if(subject.isAuthenticated()){
            return new Result("登录成功");
        }
        UsernamePasswordToken t = new UsernamePasswordToken(username, password);
        t.setRememberMe(rememberMe);
        try{
            subject.login(t);
        }catch (UnknownAccountException e1){
            throw new UnknownAccountException();
        } catch (IncorrectCredentialsException e){
            throw new IncorrectCredentialsException();
        } catch (AuthenticationException e){
            throw new AuthenticationException();
        }
        return new Result("登录成功");
    }
    @RequestMapping("index")
    public String index(){
        return "index";
    }
    @RequestMapping("logout")
    @ResponseBody
    public Result logout(){
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        return new Result("退出成功");
    }
//    =================Resource======================
    @RequestMapping("resource/manager")
    public String resourceManager(){
        return "admin/resource/resource";
    }
    @RequestMapping("resource/addPage")
    public String resourceAdd(){
        return "admin/resource/resourceAdd";
    }
    @RequestMapping("resource/editPage")
    public String  resourceEdit(Long id,HttpServletRequest request){
        request.setAttribute("resource",resourceService.selectById(id));
        return "admin/resource/resourceEdit";
    }

    //    =================Roll======================
    @RequestMapping("role/manager")
    public String roleManager(){
        return "admin/role/role";
    }
    @RequestMapping("role/addPage")
    public String roleAdd(){
        return "admin/role/roleAdd";
    }
    @RequestMapping("role/editPage")
    public String roleEdit(Long id,HttpServletRequest request){
        request.setAttribute("role",roleService.selectById(id));
        return "admin/role/roleEdit";
    }
    @RequestMapping("role/grantPage")
    public String roleGrant(Long id,HttpServletRequest request){
        request.setAttribute("id",id);
        return "admin/role/roleGrant";
    }

    //    =================User======================
    @RequestMapping("user/manager")
    public String userManager(){
        return "admin/user/user";
    }
    @RequestMapping("user/addPage")
    public String userAdd(){
        return "admin/user/userAdd";
    }
    @RequestMapping("user/editPage")
    public String userEdit(Long id,HttpServletRequest request){
        request.setAttribute("user",userService.selectById(id));
        return "admin/user/userEdit";
    }

    //    =================organization======================
    @RequestMapping("organization/manager")
    public String organizationManager(){
        return "admin/organization/organization";
    }
    @RequestMapping("organization/addPage")
    public String organizationAdd(){
        return "admin/organization/organizationAdd";
    }
    @RequestMapping("organization/editPage")
    public String organizationEdit(Long id,HttpServletRequest request){
        request.setAttribute("organization",organizationService.selectById(id));
        return "admin/organization/organizationEdit";
    }
    //  ===================Article==========================
    @RequestMapping("tbArticle/manager")
    public String tbArticleManager(){
        return "admin/tbArticle/tbArticleList";
    }
    @RequestMapping("tbArticle/addPage")
    public String articleAddPage(){
        return "admin/tbArticle/tbArticleAdd";
    }
}
